/*
* Created by: yyf
* Description: 基金管理--拟设立基金添加页
*/
<template>
    <div>
        <div class="right ca-p-t-50">
            <el-row>
                <el-col :span="24" class="ca-align-right">
                    <div class="ca-head-fixed">
                        <el-button type="primary" @click="fundAdd()">保存</el-button>
                        <el-button @click="exit()">退出</el-button>
                    </div>
                </el-col>
            </el-row>
            <div class="ca-box ca-p-20">
                <el-form :rules="rules" ref="ruleForm" label-width="100px" :model="fundEntity">
                    <el-row>
                        <el-col :span="7">
                            <el-form-item label="基金全称" prop="fundFullname">
                                <el-input v-model="fundEntity.fundFullname" :maxlength="100"></el-input>
                            </el-form-item>
                            <el-form-item label="拟设立规模" prop="fundScale">
                                <el-input v-model="fundEntity.fundScale" placeholder="万元" :maxlength="21"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7" :offset="1">
                            <el-form-item label="基金简称" prop="fundName">
                                <el-input v-model="fundEntity.fundName" :maxlength="100"></el-input>
                            </el-form-item>
                            <el-form-item label="拟设立日期">
                                <el-date-picker
                                    type="date"
                                    v-model="fundEntity.establishTime"
                                    placeholder="选择日期"
                                    style="width:100%" >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7" :offset="1">
                            <el-form-item label="基金类型">
                                <el-select v-model="fundEntity.fundType" placeholder="请选择基金类型" class="ca-w-100">
                                    <el-option
                                        v-for="item in initView.fundType"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="投资期" prop="investmentPeriod">
                                <el-input placeholder="年" type="number" v-model="fundEntity.investmentPeriod"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="7">
                            <el-form-item label="退出期" prop="exitPeriod">
                                <el-input placeholder="年" type="number" v-model="fundEntity.exitPeriod"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7" :offset="1">
                            <el-form-item label="投资方向">
                                <el-select
                                    v-model="industryItem"
                                    filterable
                                    @change="changeIndustry"
                                    placeholder="请选择投资方向"
                                    style="width:100%;">
                                    <el-option
                                        v-for="item in initView.industry"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label-width="30px">
                                <el-tag
                                    v-for="item in industryList"
                                    :key="item.id"
                                    closable
                                    :disable-transitions="false"
                                    class="ca-m-r-5"
                                    @close="handleClose(item)">
                                    {{item.industry | getNameById('industry')}}
                                </el-tag>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="7">
                            <el-form-item label="基金经理">
                                <el-select v-model="fundUser"
                                           filterable
                                           @change="changeUser"
                                           class="ca-w-100"
                                           placeholder="请选择">
                                    <el-option
                                        v-for="item in initView.userEntityList"
                                        :key="item.id"
                                        :label="item.userName"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7" :offset="1">
                            <el-form-item label="基金管理团队">
                                <el-select v-model="fundUserTeam"
                                           filterable
                                           @change="changeUserTeam"
                                           placeholder="请选择"
                                           style="width:100%;">
                                    <el-option
                                        v-for="item in initView.userEntityList"
                                        :key="item.id"
                                        :label="item.userName"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item  label-width="30px">
                                <el-tag
                                    v-for="item in fundUserList"
                                    v-if="item.userType==='2'"
                                    :key="item.id"
                                    closable
                                    :disable-transitions="false"
                                    class="ca-m-r-5"
                                    @close="handleCloseUserTeam(item)">
                                    {{item.userName}}
                                </el-tag>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div class="ca-box ca-m-t-10 ca-p-20">
                <div class="ca-title ca-m-b-20">
                    <h3 class="tab-pane-title">基本信息</h3>
                </div>
                <el-form label-width="100px">
                    <el-row>
                        <el-col :span="11">
                            <el-form-item label="基金托管人">
                                <el-input v-model="fundAnsatEntity.custodian" :maxlength="300" style="width:100%;"></el-input>
                            </el-form-item>
                            <el-form-item label="注册地">
                                <el-input v-model="fundAnsatEntity.place" :maxlength="300" style="width:100%;"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="11" class="ca-pull-right">
                            <el-form-item label="基金委派代表">
                                <el-input v-model="fundAnsatEntity.fundDelegate" :maxlength="300" style="width:100%;"></el-input>
                            </el-form-item>
                            <el-form-item label="办公地">
                                <el-input v-model="fundAnsatEntity.officeLand" :maxlength="300" style="width:100%;"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-form-item label="管理费">
                            <el-input type="textarea" :row="4" :maxlength="3000" v-model="fundAnsatEntity.proportion"></el-input>
                        </el-form-item>
                    </el-row>
                    <el-row>
                        <el-form-item label="业绩报酬">
                            <el-input type="textarea" :row="4" :maxlength="3000" v-model="fundAnsatEntity.achievement"></el-input>
                        </el-form-item>
                    </el-row>
                    <el-row>
                        <el-form-item label="其他">
                            <el-input type="textarea" :row="4" :maxlength="3000" v-model="fundAnsatEntity.other"></el-input>
                        </el-form-item>
                    </el-row>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from './api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import { addRemind } from '__service/utils';
    export default {
        data() {
            var validateNumber = (rule, value, callback) => {
                var reg = /^\d{1,10}(\.\d{1,10})?$/;
                if (value && (!reg.test(value))) {
                    callback(new Error('请输入正确范围的数字'));
                } else {
                    callback();
                }
            };
            return {
                id: '', // 添加返回的id
                initView: {
                    fundType: this.$store.state.user.dictionaryMap.fundType,// 基金类型
                    industry: this.$store.state.user.dictionaryMap.industry, // 投资方向
                    userEntityList: this.$store.state.user.department.userEntityList,// 客户
                },
                isAuthEdit: false,
                fundAnsatEntity: {  // 其他信息
                    achievement: '', // 业绩报酬
                    custodian: '', // 基金托管人
                    firstContact: '', // 初次接触日期
                    fundId: '', // 主id
                    fundSource: '', // 基金来源
                    officeLand: '', // 办公地
                    id: '', //
                    lastContact: '', // 最后接触日期
                    proportion: '', // 管理费比例
                    partnerId: '', // 执行事务合伙人
                    partnerName: '', // 合伙人姓名
                    progress: '',//进展说明
                    place: '', // 注册地
                    fundDelegate: '', // 委派代表
                    thresholdYield: '', // 门槛收益率
                    other: '', // 其他
                },
                fundEntity: {  // 基金基本信息
                    establishTime: '',// 拟设立日期(或 成立时间 )
                    exitPeriod: '', // 退出期
                    exitTime: '', // 退出时间
                    fundCategory: '', // 基金类别 对应字典表（d_type=fundCategory）
                    fundFullname: '', // 基金全称
                    fundName: '', // 基金简称
                    fundScale: null, // 基金规模(或 拟设立规模)
                    fundState: 0, // 设立状态：0：拟设立，1：已设立，2：已到期（或已退出）,4:母基金 对应字典表（d_type=fundState） ,
                    fundType: '', // 基金类型:对应字典表（d_type=fundType）
                    id: '', // 基金主id
                    investmentPeriod: '', // 投资期
                    treeType: '1', // 1：子基金；2：母基金 对应字典表：fundTree
                },
                fundUserList: [], // 基金管理团队
                fundUser: '', // 基金经理id
                fundUserTeam: '', // 单个基金管理团队id
                industryItem: '', // 投资方向id
                industryList: [], // 投资方向
                rules: {
                    fundFullname: [
                        { required: true, message: '请输入基金全称', trigger: 'blur' },
                    ],
                    fundName: [
                        { required: true, message: '请输入基金简称', trigger: 'blur' },
                    ],
                    investmentPeriod: [
                        { required: true, message: '请输入投资期', trigger: 'blur' },
                        { max: 14, message: '字数限制0~14位', trigger: 'blur'}
                    ],
                    exitPeriod: [
                        { required: true, message: '请输入退出期', trigger: 'blur' },
                        { max: 14, message: '字数限制0~14位', trigger: 'blur'}
                    ],
                    fundScale: [
                        { validator: validateNumber, trigger: 'blur' }
                    ],
                },
            }
        },
        props: {
        },
        mounted() {
        },
        methods: {
            // 保存
            fundAdd() {
                this.$refs.ruleForm.validate(async (valid) => {
                    if (valid) {
                        let param = {
                            fundAnsatEntity: this.fundAnsatEntity,
                            fundEntity: this.fundEntity,
                            fundUserList: this.fundUserList,
                            industryList: this.industryList
                        }
                        let {resCode, data} = await apiHttp(api.ESTABLISHING_FUNDS_ADD, param);
                        if (resCode === STATUS_HTTP_SUCCESS) {
                            this.id = data.id;
                            this.$message({
                                type: 'success',
                                message: '添加成功!'
                            });
                            // 添加小红点
                            await addRemind({
                                dataId: data.id, // 业务id
                                dataTab: 0, // tab页顺序 head 也为0 新建为'-1'
                                dataType: 4, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                            });
                            // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                            this.$root.Dot.$emit('getDotList');
                            // 添加小红点 end
                            this.close();
                        }
                    } else {
                        return false;
                    }
                })
            },
            // 退出新增页面
            exit() {
                this.$confirm('是否退出', '提示', {
                    cancelButtonText: '否',
                    confirmButtonText: '是',
                    type: 'warning',
                    showClose: false
                }).then(() => {
                    this.close();
                })
            },
            // 返回列表页
            close() {
                this.$router.push({name: 'establishing-funds-list', params: {openWindow: false}})
            },
            // 关闭投资方向标签
            handleClose(tag) {
                this.industryList.splice(this.industryList.indexOf(tag), 1);
            },
            // 选择投资方向
            changeIndustry(val) {
                let noRepeat = [];
                if (this.industryList.length > 0 ) {//数组中已有数据，判断是否重复
                    noRepeat = this.industryList.map(industryItem=>{
                        return industryItem.industry;
                    })
                }
                if (noRepeat.indexOf(val) === -1) {//没有重复，添加
                    this.industryList.push({
                        fundId: this.itemId,
                        id: '',
                        industry: val,
                    })
                }
            },
            // 改变基金经理
            changeUser(val) {
                this.fundUserList.map((userItem, index)=>{//如果基金经理存在，就清空
                    if (userItem.userType === '1') {
                        this.fundUserList.splice(index,1)
                    }
                })
                this.initView.userEntityList.map(user=>{//更改基金经理
                    if (user.id === val) {
                        this.fundUserList.push({
                            fundId: '', //基金主id
                            id: '',    //主id
                            userId: val,  //用户id
                            userName: user.userName,  //用户名称
                            userType: '1',  //用户类型  1：基金经理；2：基金管理团队
                        })
                    }
                })
            },
            //关闭管理团队标签
            handleCloseUserTeam(tag) {
                this.fundUserList.splice(this.fundUserList.indexOf(tag), 1);
            },
            // 选择基金管理团队
            changeUserTeam(teamId) {
                let noRepeat = [];
                if (this.fundUserList.length > 0 ) {//数组中已有数据，判断是否重复
                    this.fundUserList.map(user => {
                        if (user.userType === '2') {
                            noRepeat.push(user.userId);
                        }
                    })
                }
                if (noRepeat.indexOf(teamId) === -1) {//没有重复，添加
                    this.initView.userEntityList.map(user=>{//更改基金经理
                        if (user.id === teamId) {
                            this.fundUserList.push({
                                fundId: '', //基金主id
                                id: '',    //主id
                                userId: teamId,  //用户id
                                userName: user.userName,  //用户名称
                                userType: '2',  //用户类型  1：基金经理；2：基金管理团队
                            })
                        }
                    })
                }
            }
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss">
    .ca-w-100 {
        width: 100%;
    }
</style>
